Last Updated 05 May 99 Operator Precedence
PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Operator Precedence

AppleScript allows you to combine expressions into larger, more complex expressions. When evaluating expressions, AppleScript uses operator precedence to determine which operations are performed first. Table 6-2 shows the order in which AppleScript performs operations.

To see how operator precedence works, consider the following expression.

2 * 5 + 12
--result: 22

To evaluate the expression, AppleScript performs the multiplication operation 2 * 5 first, because as shown in Table 6-2, multiplication has higher precedence than addition.

The column labeled "Associativity" in Table 6-2 indicates the order in which AppleScript performs operations if there are two or more operations of the same precedence in an expression. The word "none" in the Associativity column indicates that you cannot have multiple consecutive occurrences of the operation in an expression. For example, the expression 3 = 3 = 3 is not legal because the associativity for the equal operator (=) is "none." The word "unary" indicates that the operator is a unary operator. To evaluate expressions with multiple unary operators of the same order, AppleScript applies the operator closest to the operand first, then applies the next closest operator, and so on. For example, the expression not not not true is evaluated as not (not (not true)) .

Table 6-2   Operator precedence 

Order

Operators

Associativity

Type of operator

1 ( ) Innermost to outermost Grouping
2 +
-
Unary Plus or minus sign for numbers
3 ^ Right to left Exponentiation
4 *
/
÷
div
mod
Left to right Multiplication and division
5 +
-
Left to right Addition and subtraction
6 & Left to right Concatenation
7 as Left to right Coercion
8 <

>
None Comparison
9 =
None Equality and inequality
10 not Unary Logical negation
11 and Left to right Logical for Boolean values
12 or Left to right Logical for Boolean values

You can change the order in which AppleScript performs operations by grouping expressions in parentheses. As shown in Table 6-2, AppleScript evaluates expressions in parentheses first. For example, adding parentheses around 5 + 12 in the following expression causes AppleScript to perform the addition operation first and changes the result.

2 * ( 5 + 12 )
--result: 34

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)